﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHISInteroperability.Timeframe
{
    public class Period
    {
        public DateTime StartDate { get; set; }
        public PeriodFormat Format { get; set; }

        public DateTime? EndDate { get; set; }

        public string GetFormattedPeriod()
        {
            string period = string.Empty;

            switch (Format)
            {
                case PeriodFormat.DAILY:
                    period = string.Format("{0:yyyyMMdd}", StartDate);
                    break;

                case PeriodFormat.WEEKLY:
                    DateTimeFormatInfo dateTimeFormatInfo = DateTimeFormatInfo.CurrentInfo;
                    Calendar calendar = dateTimeFormatInfo.Calendar;
                    int week = calendar.GetWeekOfYear(StartDate, dateTimeFormatInfo.CalendarWeekRule, dateTimeFormatInfo.FirstDayOfWeek);
                    period = string.Format("{0:yyyy}W{1}", StartDate, week);
                    break;

                case PeriodFormat.MONTHLY:
                    period = string.Format("{0:yyyyMM}", StartDate);
                    break;

                case PeriodFormat.QUARTERLY:
                    int month = StartDate.Month;
                    int quarter = 1;
                    if (month > 3) quarter = 2;
                    if (month > 6) quarter = 3;
                    if (month > 9) quarter = 4;
                    period = string.Format("{0:yyyy}Q{1}", StartDate, quarter);
                    break;

                default:
                    throw new NotImplementedException("This period format is not implemented");
            }

            return period;
        }
    }
}
